home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / fcrypt.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  24KB  |  667 lines

  1. /* lib/des/fcrypt.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include <stdio.h>
  49.  
  50. /* Eric Young.
  51.  * This version of crypt has been developed from my MIT compatable
  52.  * DES library.
  53.  * The library is available at pub/Crypto/DES at ftp.psy.uq.oz.au
  54.  * eay@mincom.oz.au or eay@psych.psy.uq.oz.au
  55.  */
  56.  
  57. #if !defined(_LIBC) || defined(NOCONST)
  58. #define const
  59. #endif
  60.  
  61. typedef unsigned char des_cblock[8];
  62.  
  63. typedef struct des_ks_struct
  64.     {
  65.     union    {
  66.         des_cblock _;
  67.         /* make sure things are correct size on machines with
  68.          * 8 byte longs */
  69.         unsigned long pad[2];
  70.         } ks;
  71. #define _    ks._
  72.     } des_key_schedule[16];
  73.  
  74. #define DES_KEY_SZ     (sizeof(des_cblock))
  75. #define DES_ENCRYPT    1
  76. #define DES_DECRYPT    0
  77.  
  78. #define ITERATIONS 16
  79. #define HALF_ITERATIONS 8
  80.  
  81. #define c2l(c,l)    (l =((unsigned long)(*((c)++)))    , \
  82.              l|=((unsigned long)(*((c)++)))<< 8, \
  83.              l|=((unsigned long)(*((c)++)))<<16, \
  84.              l|=((unsigned long)(*((c)++)))<<24)
  85.  
  86. #define l2c(l,c)    (*((c)++)=(unsigned char)(((l)    )&0xff), \
  87.              *((c)++)=(unsigned char)(((l)>> 8)&0xff), \
  88.              *((c)++)=(unsigned char)(((l)>>16)&0xff), \
  89.              *((c)++)=(unsigned char)(((l)>>24)&0xff))
  90.  
  91. static const unsigned long SPtrans[8][64]={
  92. {
  93. /* nibble 0 */
  94. 0x00820200L, 0x00020000L, 0x80800000L, 0x80820200L,
  95. 0x00800000L, 0x80020200L, 0x80020000L, 0x80800000L,
  96. 0x80020200L, 0x00820200L, 0x00820000L, 0x80000200L,
  97. 0x80800200L, 0x00800000L, 0x00000000L, 0x80020000L,
  98. 0x00020000L, 0x80000000L, 0x00800200L, 0x00020200L,
  99. 0x80820200L, 0x00820000L, 0x80000200L, 0x00800200L,
  100. 0x80000000L, 0x00000200L, 0x00020200L, 0x80820000L,
  101. 0x00000200L, 0x80800200L, 0x80820000L, 0x00000000L,
  102. 0x00000000L, 0x80820200L, 0x00800200L, 0x80020000L,
  103. 0x00820200L, 0x00020000L, 0x80000200L, 0x00800200L,
  104. 0x80820000L, 0x00000200L, 0x00020200L, 0x80800000L,
  105. 0x80020200L, 0x80000000L, 0x80800000L, 0x00820000L,
  106. 0x80820200L, 0x00020200L, 0x00820000L, 0x80800200L,
  107. 0x00800000L, 0x80000200L, 0x80020000L, 0x00000000L,
  108. 0x00020000L, 0x00800000L, 0x80800200L, 0x00820200L,
  109. 0x80000000L, 0x80820000L, 0x00000200L, 0x80020200L,
  110. },{
  111. /* nibble 1 */
  112. 0x10042004L, 0x00000000L, 0x00042000L, 0x10040000L,
  113. 0x10000004L, 0x00002004L, 0x10002000L, 0x00042000L,
  114. 0x00002000L, 0x10040004L, 0x00000004L, 0x10002000L,
  115. 0x00040004L, 0x10042000L, 0x10040000L, 0x00000004L,
  116. 0x00040000L, 0x10002004L, 0x10040004L, 0x00002000L,
  117. 0x00042004L, 0x10000000L, 0x00000000L, 0x00040004L,
  118. 0x10002004L, 0x00042004L, 0x10042000L, 0x10000004L,
  119. 0x10000000L, 0x00040000L, 0x00002004L, 0x10042004L,
  120. 0x00040004L, 0x10042000L, 0x10002000L, 0x00042004L,
  121. 0x10042004L, 0x00040004L, 0x10000004L, 0x00000000L,
  122. 0x10000000L, 0x00002004L, 0x00040000L, 0x10040004L,
  123. 0x00002000L, 0x10000000L, 0x00042004L, 0x10002004L,
  124. 0x10042000L, 0x00002000L, 0x00000000L, 0x10000004L,
  125. 0x00000004L, 0x10042004L, 0x00042000L, 0x10040000L,
  126. 0x10040004L, 0x00040000L, 0x00002004L, 0x10002000L,
  127. 0x10002004L, 0x00000004L, 0x10040000L, 0x00042000L,
  128. },{
  129. /* nibble 2 */
  130. 0x41000000L, 0x01010040L, 0x00000040L, 0x41000040L,
  131. 0x40010000L, 0x01000000L, 0x41000040L, 0x00010040L,
  132. 0x01000040L, 0x00010000L, 0x01010000L, 0x40000000L,
  133. 0x41010040L, 0x40000040L, 0x40000000L, 0x41010000L,
  134. 0x00000000L, 0x40010000L, 0x01010040L, 0x00000040L,
  135. 0x40000040L, 0x41010040L, 0x00010000L, 0x41000000L,
  136. 0x41010000L, 0x01000040L, 0x40010040L, 0x01010000L,
  137. 0x00010040L, 0x00000000L, 0x01000000L, 0x40010040L,
  138. 0x01010040L, 0x00000040L, 0x40000000L, 0x00010000L,
  139. 0x40000040L, 0x40010000L, 0x01010000L, 0x41000040L,
  140. 0x00000000L, 0x01010040L, 0x00010040L, 0x41010000L,
  141. 0x40010000L, 0x01000000L, 0x41010040L, 0x40000000L,
  142. 0x40010040L, 0x41000000L, 0x01000000L, 0x41010040L,
  143. 0x00010000L, 0x01000040L, 0x41000040L, 0x00010040L,
  144. 0x01000040L, 0x00000000L, 0x41010000L, 0x40000040L,
  145. 0x41000000L, 0x40010040L, 0x00000040L, 0x01010000L,
  146. },{
  147. /* nibble 3 */
  148. 0x00100402L, 0x04000400L, 0x00000002L, 0x04100402L,
  149. 0x00000000L, 0x04100000L, 0x04000402L, 0x00100002L,
  150. 0x04100400L, 0x04000002L, 0x04000000L, 0x00000402L,
  151. 0x04000002L, 0x00100402L, 0x00100000L, 0x04000000L,
  152. 0x04100002L, 0x00100400L, 0x00000400L, 0x00000002L,
  153. 0x00100400L, 0x04000402L, 0x04100000L, 0x00000400L,
  154. 0x00000402L, 0x00000000L, 0x00100002L, 0x04100400L,
  155. 0x04000400L, 0x04100002L, 0x04100402L, 0x00100000L,
  156. 0x04100002L, 0x00000402L, 0x00100000L, 0x04000002L,
  157. 0x00100400L, 0x04000400L, 0x00000002L, 0x04100000L,
  158. 0x04000402L, 0x00000000L, 0x00000400L, 0x00100002L,
  159. 0x00000000L, 0x04100002L, 0x04100400L, 0x00000400L,
  160. 0x04000000L, 0x04100402L, 0x00100402L, 0x00100000L,
  161. 0x04100402L, 0x00000002L, 0x04000400L, 0x00100402L,
  162. 0x00100002L, 0x00100400L, 0x04100000L, 0x04000402L,
  163. 0x00000402L, 0x04000000L, 0x04000002L, 0x04100400L,
  164. },{
  165. /* nibble 4 */
  166. 0x02000000L, 0x00004000L, 0x00000100L, 0x02004108L,
  167. 0x02004008L, 0x02000100L, 0x00004108L, 0x02004000L,
  168. 0x00004000L, 0x00000008L, 0x02000008L, 0x00004100L,
  169. 0x02000108L, 0x02004008L, 0x02004100L, 0x00000000L,
  170. 0x00004100L, 0x02000000L, 0x00004008L, 0x00000108L,
  171. 0x02000100L, 0x00004108L, 0x00000000L, 0x02000008L,
  172. 0x00000008L, 0x02000108L, 0x02004108L, 0x00004008L,
  173. 0x02004000L, 0x00000100L, 0x00000108L, 0x02004100L,
  174. 0x02004100L, 0x02000108L, 0x00004008L, 0x02004000L,
  175. 0x00004000L, 0x00000008L, 0x02000008L, 0x02000100L,
  176. 0x02000000L, 0x00004100L, 0x02004108L, 0x00000000L,
  177. 0x00004108L, 0x02000000L, 0x00000100L, 0x00004008L,
  178. 0x02000108L, 0x00000100L, 0x00000000L, 0x02004108L,
  179. 0x02004008L, 0x02004100L, 0x00000108L, 0x00004000L,
  180. 0x00004100L, 0x02004008L, 0x02000100L, 0x00000108L,
  181. 0x00000008L, 0x00004108L, 0x02004000L, 0x02000008L,
  182. },{
  183. /* nibble 5 */
  184. 0x20000010L, 0x00080010L, 0x00000000L, 0x20080800L,
  185. 0x00080010L, 0x00000800L, 0x20000810L, 0x00080000L,
  186. 0x00000810L, 0x20080810L, 0x00080800L, 0x20000000L,
  187. 0x20000800L, 0x20000010L, 0x20080000L, 0x00080810L,
  188. 0x00080000L, 0x20000810L, 0x20080010L, 0x00000000L,
  189. 0x00000800L, 0x00000010L, 0x20080800L, 0x20080010L,
  190. 0x20080810L, 0x20080000L, 0x20000000L, 0x00000810L,
  191. 0x00000010L, 0x00080800L, 0x00080810L, 0x20000800L,
  192. 0x00000810L, 0x20000000L, 0x20000800L, 0x00080810L,
  193. 0x20080800L, 0x00080010L, 0x00000000L, 0x20000800L,
  194. 0x20000000L, 0x00000800L, 0x20080010L, 0x00080000L,
  195. 0x00080010L, 0x20080810L, 0x00080800L, 0x00000010L,
  196. 0x20080810L, 0x00080800L, 0x00080000L, 0x20000810L,
  197. 0x20000010L, 0x20080000L, 0x00080810L, 0x00000000L,
  198. 0x00000800L, 0x20000010L, 0x20000810L, 0x20080800L,
  199. 0x20080000L, 0x00000810L, 0x00000010L, 0x20080010L,
  200. },{
  201. /* nibble 6 */
  202. 0x00001000L, 0x00000080L, 0x00400080L, 0x00400001L,
  203. 0x00401081L, 0x00001001L, 0x00001080L, 0x00000000L,
  204. 0x00400000L, 0x00400081L, 0x00000081L, 0x00401000L,
  205. 0x00000001L, 0x00401080L, 0x00401000L, 0x00000081L,
  206. 0x00400081L, 0x00001000L, 0x00001001L, 0x00401081L,
  207. 0x00000000L, 0x00400080L, 0x00400001L, 0x00001080L,
  208. 0x00401001L, 0x00001081L, 0x00401080L, 0x00000001L,
  209. 0x00001081L, 0x00401001L, 0x00000080L, 0x00400000L,
  210. 0x00001081L, 0x00401000L, 0x00401001L, 0x00000081L,
  211. 0x00001000L, 0x00000080L, 0x00400000L, 0x00401001L,
  212. 0x00400081L, 0x00001081L, 0x00001080L, 0x00000000L,
  213. 0x00000080L, 0x00400001L, 0x00000001L, 0x00400080L,
  214. 0x00000000L, 0x00400081L, 0x00400080L, 0x00001080L,
  215. 0x00000081L, 0x00001000L, 0x00401081L, 0x00400000L,
  216. 0x00401080L, 0x00000001L, 0x00001001L, 0x00401081L,
  217. 0x00400001L, 0x00401080L, 0x00401000L, 0x00001001L,
  218. },{
  219. /* nibble 7 */
  220. 0x08200020L, 0x08208000L, 0x00008020L, 0x00000000L,
  221. 0x08008000L, 0x00200020L, 0x08200000L, 0x08208020L,
  222. 0x00000020L, 0x08000000L, 0x00208000L, 0x00008020L,
  223. 0x00208020L, 0x08008020L, 0x08000020L, 0x08200000L,
  224. 0x00008000L, 0x00208020L, 0x00200020L, 0x08008000L,
  225. 0x08208020L, 0x08000020L, 0x00000000L, 0x00208000L,
  226. 0x08000000L, 0x00200000L, 0x08008020L, 0x08200020L,
  227. 0x00200000L, 0x00008000L, 0x08208000L, 0x00000020L,
  228. 0x00200000L, 0x00008000L, 0x08000020L, 0x08208020L,
  229. 0x00008020L, 0x08000000L, 0x00000000L, 0x00208000L,
  230. 0x08200020L, 0x08008020L, 0x08008000L, 0x00200020L,
  231. 0x08208000L, 0x00000020L, 0x00200020L, 0x08008000L,
  232. 0x08208020L, 0x00200000L, 0x08200000L, 0x08000020L,
  233. 0x00208000L, 0x00008020L, 0x08008020L, 0x08200000L,
  234. 0x00000020L, 0x08208000L, 0x00208020L, 0x00000000L,
  235. 0x08000000L, 0x08200020L, 0x00008000L, 0x00208020L}};
  236. static const unsigned long skb[8][64]={
  237. {
  238. /* for C bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  239. 0x00000000L,0x00000010L,0x20000000L,0x20000010L,
  240. 0x00010000L,0x00010010L,0x20010000L,0x20010010L,
  241. 0x00000800L,0x00000810L,0x20000800L,0x20000810L,
  242. 0x00010800L,0x00010810L,0x20010800L,0x20010810L,
  243. 0x00000020L,0x00000030L,0x20000020L,0x20000030L,
  244. 0x00010020L,0x00010030L,0x20010020L,0x20010030L,
  245. 0x00000820L,0x00000830L,0x20000820L,0x20000830L,
  246. 0x00010820L,0x00010830L,0x20010820L,0x20010830L,
  247. 0x00080000L,0x00080010L,0x20080000L,0x20080010L,
  248. 0x00090000L,0x00090010L,0x20090000L,0x20090010L,
  249. 0x00080800L,0x00080810L,0x20080800L,0x20080810L,
  250. 0x00090800L,0x00090810L,0x20090800L,0x20090810L,
  251. 0x00080020L,0x00080030L,0x20080020L,0x20080030L,
  252. 0x00090020L,0x00090030L,0x20090020L,0x20090030L,
  253. 0x00080820L,0x00080830L,0x20080820L,0x20080830L,
  254. 0x00090820L,0x00090830L,0x20090820L,0x20090830L,
  255. },{
  256. /* for C bits (numbered as per FIPS 46) 7 8 10 11 12 13 */
  257. 0x00000000L,0x02000000L,0x00002000L,0x02002000L,
  258. 0x00200000L,0x02200000L,0x00202000L,0x02202000L,
  259. 0x00000004L,0x02000004L,0x00002004L,0x02002004L,
  260. 0x00200004L,0x02200004L,0x00202004L,0x02202004L,
  261. 0x00000400L,0x02000400L,0x00002400L,0x02002400L,
  262. 0x00200400L,0x02200400L,0x00202400L,0x02202400L,
  263. 0x00000404L,0x02000404L,0x00002404L,0x02002404L,
  264. 0x00200404L,0x02200404L,0x00202404L,0x02202404L,
  265. 0x10000000L,0x12000000L,0x10002000L,0x12002000L,
  266. 0x10200000L,0x12200000L,0x10202000L,0x12202000L,
  267. 0x10000004L,0x12000004L,0x10002004L,0x12002004L,
  268. 0x10200004L,0x12200004L,0x10202004L,0x12202004L,
  269. 0x10000400L,0x12000400L,0x10002400L,0x12002400L,
  270. 0x10200400L,0x12200400L,0x10202400L,0x12202400L,
  271. 0x10000404L,0x12000404L,0x10002404L,0x12002404L,
  272. 0x10200404L,0x12200404L,0x10202404L,0x12202404L,
  273. },{
  274. /* for C bits (numbered as per FIPS 46) 14 15 16 17 19 20 */
  275. 0x00000000L,0x00000001L,0x00040000L,0x00040001L,
  276. 0x01000000L,0x01000001L,0x01040000L,0x01040001L,
  277. 0x00000002L,0x00000003L,0x00040002L,0x00040003L,
  278. 0x01000002L,0x01000003L,0x01040002L,0x01040003L,
  279. 0x00000200L,0x00000201L,0x00040200L,0x00040201L,
  280. 0x01000200L,0x01000201L,0x01040200L,0x01040201L,
  281. 0x00000202L,0x00000203L,0x00040202L,0x00040203L,
  282. 0x01000202L,0x01000203L,0x01040202L,0x01040203L,
  283. 0x08000000L,0x08000001L,0x08040000L,0x08040001L,
  284. 0x09000000L,0x09000001L,0x09040000L,0x09040001L,
  285. 0x08000002L,0x08000003L,0x08040002L,0x08040003L,
  286. 0x09000002L,0x09000003L,0x09040002L,0x09040003L,
  287. 0x08000200L,0x08000201L,0x08040200L,0x08040201L,
  288. 0x09000200L,0x09000201L,0x09040200L,0x09040201L,
  289. 0x08000202L,0x08000203L,0x08040202L,0x08040203L,
  290. 0x09000202L,0x09000203L,0x09040202L,0x09040203L,
  291. },{
  292. /* for C bits (numbered as per FIPS 46) 21 23 24 26 27 28 */
  293. 0x00000000L,0x00100000L,0x00000100L,0x00100100L,
  294. 0x00000008L,0x00100008L,0x00000108L,0x00100108L,
  295. 0x00001000L,0x00101000L,0x00001100L,0x00101100L,
  296. 0x00001008L,0x00101008L,0x00001108L,0x00101108L,
  297. 0x04000000L,0x04100000L,0x04000100L,0x04100100L,
  298. 0x04000008L,0x04100008L,0x04000108L,0x04100108L,
  299. 0x04001000L,0x04101000L,0x04001100L,0x04101100L,
  300. 0x04001008L,0x04101008L,0x04001108L,0x04101108L,
  301. 0x00020000L,0x00120000L,0x00020100L,0x00120100L,
  302. 0x00020008L,0x00120008L,0x00020108L,0x00120108L,
  303. 0x00021000L,0x00121000L,0x00021100L,0x00121100L,
  304. 0x00021008L,0x00121008L,0x00021108L,0x00121108L,
  305. 0x04020000L,0x04120000L,0x04020100L,0x04120100L,
  306. 0x04020008L,0x04120008L,0x04020108L,0x04120108L,
  307. 0x04021000L,0x04121000L,0x04021100L,0x04121100L,
  308. 0x04021008L,0x04121008L,0x04021108L,0x04121108L,
  309. },{
  310. /* for D bits (numbered as per FIPS 46) 1 2 3 4 5 6 */
  311. 0x00000000L,0x10000000L,0x00010000L,0x10010000L,
  312. 0x00000004L,0x10000004L,0x00010004L,0x10010004L,
  313. 0x20000000L,0x30000000L,0x20010000L,0x30010000L,
  314. 0x20000004L,0x30000004L,0x20010004L,0x30010004L,
  315. 0x00100000L,0x10100000L,0x00110000L,0x10110000L,
  316. 0x00100004L,0x10100004L,0x00110004L,0x10110004L,
  317. 0x20100000L,0x30100000L,0x20110000L,0x30110000L,
  318. 0x20100004L,0x30100004L,0x20110004L,0x30110004L,
  319. 0x00001000L,0x10001000L,0x00011000L,0x10011000L,
  320. 0x00001004L,0x10001004L,0x00011004L,0x10011004L,
  321. 0x20001000L,0x30001000L,0x20011000L,0x30011000L,
  322. 0x20001004L,0x30001004L,0x20011004L,0x30011004L,
  323. 0x00101000L,0x10101000L,0x00111000L,0x10111000L,
  324. 0x00101004L,0x10101004L,0x00111004L,0x10111004L,
  325. 0x20101000L,0x30101000L,0x20111000L,0x30111000L,
  326. 0x20101004L,0x30101004L,0x20111004L,0x30111004L,
  327. },{
  328. /* for D bits (numbered as per FIPS 46) 8 9 11 12 13 14 */
  329. 0x00000000L,0x08000000L,0x00000008L,0x08000008L,
  330. 0x00000400L,0x08000400L,0x00000408L,0x08000408L,
  331. 0x00020000L,0x08020000L,0x00020008L,0x08020008L,
  332. 0x00020400L,0x08020400L,0x00020408L,0x08020408L,
  333. 0x00000001L,0x08000001L,0x00000009L,0x08000009L,
  334. 0x00000401L,0x08000401L,0x00000409L,0x08000409L,
  335. 0x00020001L,0x08020001L,0x00020009L,0x08020009L,
  336. 0x00020401L,0x08020401L,0x00020409L,0x08020409L,
  337. 0x02000000L,0x0A000000L,0x02000008L,0x0A000008L,
  338. 0x02000400L,0x0A000400L,0x02000408L,0x0A000408L,
  339. 0x02020000L,0x0A020000L,0x02020008L,0x0A020008L,
  340. 0x02020400L,0x0A020400L,0x02020408L,0x0A020408L,
  341. 0x02000001L,0x0A000001L,0x02000009L,0x0A000009L,
  342. 0x02000401L,0x0A000401L,0x02000409L,0x0A000409L,
  343. 0x02020001L,0x0A020001L,0x02020009L,0x0A020009L,
  344. 0x02020401L,0x0A020401L,0x02020409L,0x0A020409L,
  345. },{
  346. /* for D bits (numbered as per FIPS 46) 16 17 18 19 20 21 */
  347. 0x00000000L,0x00000100L,0x00080000L,0x00080100L,
  348. 0x01000000L,0x01000100L,0x01080000L,0x01080100L,
  349. 0x00000010L,0x00000110L,0x00080010L,0x00080110L,
  350. 0x01000010L,0x01000110L,0x01080010L,0x01080110L,
  351. 0x00200000L,0x00200100L,0x00280000L,0x00280100L,
  352. 0x01200000L,0x01200100L,0x01280000L,0x01280100L,
  353. 0x00200010L,0x00200110L,0x00280010L,0x00280110L,
  354. 0x01200010L,0x01200110L,0x01280010L,0x01280110L,
  355. 0x00000200L,0x00000300L,0x00080200L,0x00080300L,
  356. 0x01000200L,0x01000300L,0x01080200L,0x01080300L,
  357. 0x00000210L,0x00000310L,0x00080210L,0x00080310L,
  358. 0x01000210L,0x01000310L,0x01080210L,0x01080310L,
  359. 0x00200200L,0x00200300L,0x00280200L,0x00280300L,
  360. 0x01200200L,0x01200300L,0x01280200L,0x01280300L,
  361. 0x00200210L,0x00200310L,0x00280210L,0x00280310L,
  362. 0x01200210L,0x01200310L,0x01280210L,0x01280310L,
  363. },{
  364. /* for D bits (numbered as per FIPS 46) 22 23 24 25 27 28 */
  365. 0x00000000L,0x04000000L,0x00040000L,0x04040000L,
  366. 0x00000002L,0x04000002L,0x00040002L,0x04040002L,
  367. 0x00002000L,0x04002000L,0x00042000L,0x04042000L,
  368. 0x00002002L,0x04002002L,0x00042002L,0x04042002L,
  369. 0x00000020L,0x04000020L,0x00040020L,0x04040020L,
  370. 0x00000022L,0x04000022L,0x00040022L,0x04040022L,
  371. 0x00002020L,0x04002020L,0x00042020L,0x04042020L,
  372. 0x00002022L,0x04002022L,0x00042022L,0x04042022L,
  373. 0x00000800L,0x04000800L,0x00040800L,0x04040800L,
  374. 0x00000802L,0x04000802L,0x00040802L,0x04040802L,
  375. 0x00002800L,0x04002800L,0x00042800L,0x04042800L,
  376. 0x00002802L,0x04002802L,0x00042802L,0x04042802L,
  377. 0x00000820L,0x04000820L,0x00040820L,0x04040820L,
  378. 0x00000822L,0x04000822L,0x00040822L,0x04040822L,
  379. 0x00002820L,0x04002820L,0x00042820L,0x04042820L,
  380. 0x00002822L,0x04002822L,0x00042822L,0x04042822L,
  381. } };
  382.  
  383. /* See ecb_encrypt.c for a pseudo description of these macros. */
  384. #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
  385.     (b)^=(t),\
  386.     (a)^=((t)<<(n)))
  387.  
  388. #define HPERM_OP(a,t,n,m) ((t)=((((a)<<(16-(n)))^(a))&(m)),\
  389.     (a)=(a)^(t)^(t>>(16-(n))))\
  390.  
  391. static const int shifts2[16]={0,0,1,1,1,1,1,1,0,1,1,1,1,1,1,0};
  392.  
  393. #ifdef PROTO
  394. static int body(unsigned long *out0, unsigned long *out1,
  395.     des_key_schedule ks, unsigned long Eswap0, unsigned long Eswap1);
  396. static int des_set_key(des_cblock (*key), des_key_schedule schedule);
  397. #else
  398. static int body();
  399. static int des_set_key();
  400. #endif
  401.  
  402. static int des_set_key(key, schedule)
  403. des_cblock (*key);
  404. des_key_schedule schedule;
  405.     {
  406.     register unsigned long c,d,t,s;
  407.     register unsigned char *in;
  408.     register unsigned long *k;
  409.     register int i;
  410.  
  411.     k=(unsigned long *)schedule;
  412.     in=(unsigned char *)key;
  413.  
  414.     c2l(in,c);
  415.     c2l(in,d);
  416.  
  417.     /* I now do it in 47 simple operations :-)
  418.      * Thanks to John Fletcher (john_fletcher@lccmail.ocf.llnl.gov)
  419.      * for the inspiration. :-) */
  420.     PERM_OP (d,c,t,4,0x0f0f0f0fL);
  421.     HPERM_OP(c,t,-2,0xcccc0000L);
  422.     HPERM_OP(d,t,-2,0xcccc0000L);
  423.     PERM_OP (d,c,t,1,0x55555555L);
  424.     PERM_OP (c,d,t,8,0x00ff00ffL);
  425.     PERM_OP (d,c,t,1,0x55555555L);
  426.     d=    (((d&0x000000ffL)<<16)| (d&0x0000ff00L)     |
  427.          ((d&0x00ff0000L)>>16)|((c&0xf0000000L)>>4));
  428.     c&=0x0fffffffL;
  429.  
  430.     for (i=0; i<ITERATIONS; i++)
  431.         {
  432.         if (shifts2[i])
  433.             { c=((c>>2)|(c<<26)); d=((d>>2)|(d<<26)); }
  434.         else
  435.             { c=((c>>1)|(c<<27)); d=((d>>1)|(d<<27)); }
  436.         c&=0x0fffffffL;
  437.         d&=0x0fffffffL;
  438.         /* could be a few less shifts but I am to lazy at this
  439.          * point in time to investigate */
  440.         s=    skb[0][ (c     )&0x3f                 ]|
  441.             skb[1][((c>> 6L)&0x03)|((c>> 7L)&0x3c)]|
  442.             skb[2][((c>>13L)&0x0f)|((c>>14L)&0x30)]|
  443.             skb[3][((c>>20L)&0x01)|((c>>21L)&0x06) |
  444.                            ((c>>22L)&0x38)];
  445.         t=    skb[4][ (d     )&0x3f                 ]|
  446.             skb[5][((d>> 7L)&0x03)|((d>> 8L)&0x3c)]|
  447.             skb[6][ (d>>15L)&0x3f                 ]|
  448.             skb[7][((d>>21L)&0x0f)|((d>>22L)&0x30)];
  449.  
  450.         /* table contained 0213 4657 */
  451.         *(k++)=((t<<16)|(s&0x0000ffffL))&0xffffffffL;
  452.         s=     ((s>>16)|(t&0xffff0000L));
  453.         
  454.         s=(s<<4)|(s>>28);
  455.         *(k++)=s&0xffffffffL;
  456.         }
  457.     return(0);
  458.     }
  459.  
  460. /******************************************************************
  461.  * modified stuff for crypt.
  462.  ******************************************************************/
  463.  
  464. /* The changes to this macro may help or hinder, depending on the
  465.  * compiler and the achitecture.  gcc2 always seems to do well :-). 
  466.  * Inspired by Dana How <how@isl.stanford.edu>
  467.  * DO NOT use the alternative version on machines with 8 byte longs.
  468.  */
  469. #ifdef DES_USE_PTR
  470. #define D_ENCRYPT(L,R,S) \
  471.     t=(R^(R>>16)); \
  472.     u=(t&E0); \
  473.     t=(t&E1); \
  474.     u=((u^(u<<16))^R^s[S  ])<<2; \
  475.     t=(t^(t<<16))^R^s[S+1]; \
  476.     t=(t>>2)|(t<<30); \
  477.     L^= \
  478.     *(unsigned long *)(des_SP+0x0100+((t    )&0xfc))+ \
  479.     *(unsigned long *)(des_SP+0x0300+((t>> 8)&0xfc))+ \
  480.     *(unsigned long *)(des_SP+0x0500+((t>>16)&0xfc))+ \
  481.     *(unsigned long *)(des_SP+0x0700+((t>>24)&0xfc))+ \
  482.     *(unsigned long *)(des_SP+       ((u    )&0xfc))+ \
  483.       *(unsigned long *)(des_SP+0x0200+((u>> 8)&0xfc))+ \
  484.       *(unsigned long *)(des_SP+0x0400+((u>>16)&0xfc))+ \
  485.      *(unsigned long *)(des_SP+0x0600+((u>>24)&0xfc));
  486. #else /* original version */
  487. #define D_ENCRYPT(L,R,S)    \
  488.     t=(R^(R>>16)); \
  489.     u=(t&E0); \
  490.     t=(t&E1); \
  491.     u=(u^(u<<16))^R^s[S  ]; \
  492.     t=(t^(t<<16))^R^s[S+1]; \
  493.     t=(t>>4)|(t<<28); \
  494.     L^=    SPtrans[1][(t    )&0x3f]| \
  495.         SPtrans[3][(t>> 8)&0x3f]| \
  496.         SPtrans[5][(t>>16)&0x3f]| \
  497.         SPtrans[7][(t>>24)&0x3f]| \
  498.         SPtrans[0][(u    )&0x3f]| \
  499.         SPtrans[2][(u>> 8)&0x3f]| \
  500.         SPtrans[4][(u>>16)&0x3f]| \
  501.         SPtrans[6][(u>>24)&0x3f];
  502. #endif
  503.  
  504. static unsigned const char con_salt[128]={
  505. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  506. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  507. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  508. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  509. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  510. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,
  511. 0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
  512. 0x0A,0x0B,0x05,0x06,0x07,0x08,0x09,0x0A,
  513. 0x0B,0x0C,0x0D,0x0E,0x0F,0x10,0x11,0x12,
  514. 0x13,0x14,0x15,0x16,0x17,0x18,0x19,0x1A,
  515. 0x1B,0x1C,0x1D,0x1E,0x1F,0x20,0x21,0x22,
  516. 0x23,0x24,0x25,0x20,0x21,0x22,0x23,0x24,
  517. 0x25,0x26,0x27,0x28,0x29,0x2A,0x2B,0x2C,
  518. 0x2D,0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,
  519. 0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3C,
  520. 0x3D,0x3E,0x3F,0x00,0x00,0x00,0x00,0x00,
  521. };
  522.  
  523. static unsigned const char cov_2char[64]={
  524. 0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
  525. 0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
  526. 0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
  527. 0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
  528. 0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
  529. 0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
  530. 0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
  531. 0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
  532. };
  533.  
  534. #ifdef PROTO
  535. #ifdef PERL5
  536. char *des_crypt(char *buf,char *salt);
  537. #else
  538. char *crypt(char *buf,char *salt);
  539. #endif
  540. #else
  541. #ifdef PERL5
  542. char *des_crypt();
  543. #else
  544. char *crypt();
  545. #endif
  546. #endif
  547.  
  548. #ifdef PERL5
  549. char *des_crypt(buf,salt)
  550. #else
  551. char *crypt(buf,salt)
  552. #endif
  553. char *buf;
  554. char *salt;
  555.     {
  556.     unsigned int i,j,x,y;
  557.     unsigned long Eswap0=0,Eswap1=0;
  558.     unsigned long out[2],ll;
  559.     des_cblock key;
  560.     des_key_schedule ks;
  561.     static unsigned char buff[20];
  562.     unsigned char bb[9];
  563.     unsigned char *b=bb;
  564.     unsigned char c,u;
  565.  
  566.     /* eay 25/08/92
  567.      * If you call crypt("pwd","*") as often happens when you
  568.      * have * as the pwd field in /etc/passwd, the function
  569.      * returns *\0XXXXXXXXX
  570.      * The \0 makes the string look like * so the pwd "*" would
  571.      * crypt to "*".  This was found when replacing the crypt in
  572.      * our shared libraries.  People found that the disbled
  573.      * accounts effectivly had no passwd :-(. */
  574.     x=buff[0]=((salt[0] == '\0')?'A':salt[0]);
  575.     Eswap0=con_salt[x];
  576.     x=buff[1]=((salt[1] == '\0')?'A':salt[1]);
  577.     Eswap1=con_salt[x]<<4;
  578.  
  579.     for (i=0; i<8; i++)
  580.         {
  581.         c= *(buf++);
  582.         if (!c) break;
  583.         key[i]=(c<<1);
  584.         }
  585.     for (; i<8; i++)
  586.         key[i]=0;
  587.  
  588.     des_set_key((des_cblock *)(key),ks);
  589.     body(&(out[0]),&(out[1]),ks,Eswap0,Eswap1);
  590.  
  591.     ll=out[0]; l2c(ll,b);
  592.     ll=out[1]; l2c(ll,b);
  593.     y=0;
  594.     u=0x80;
  595.     bb[8]=0;
  596.     for (i=2; i<13; i++)
  597.         {
  598.         c=0;
  599.         for (j=0; j<6; j++)
  600.             {
  601.             c<<=1;
  602.             if (bb[y] & u) c|=1;
  603.             u>>=1;
  604.             if (!u)
  605.                 {
  606.                 y++;
  607.                 u=0x80;
  608.                 }
  609.             }
  610.         buff[i]=cov_2char[c];
  611.         }
  612.     buff[13]='\0';
  613.     return((char *)buff);
  614.     }
  615.  
  616. static int body(out0, out1, ks, Eswap0, Eswap1)
  617. unsigned long *out0;
  618. unsigned long *out1;
  619. des_key_schedule ks;
  620. unsigned long Eswap0;
  621. unsigned long Eswap1;
  622.     {
  623.     register unsigned long l,r,t,u;
  624. #ifdef DES_USE_PTR
  625.     register unsigned char *des_SP=(unsigned char *)SPtrans;
  626. #endif
  627.     register unsigned long *s;
  628.     register int i,j;
  629.     register unsigned long E0,E1;
  630.  
  631.     l=0;
  632.     r=0;
  633.  
  634.     s=(unsigned long *)ks;
  635.     E0=Eswap0;
  636.     E1=Eswap1;
  637.  
  638.     for (j=0; j<25; j++)
  639.         {
  640.         for (i=0; i<(ITERATIONS*2); i+=4)
  641.             {
  642.             D_ENCRYPT(l,r,  i);    /*  1 */
  643.             D_ENCRYPT(r,l,  i+2);    /*  2 */
  644.             }
  645.         t=l;
  646.         l=r;
  647.         r=t;
  648.         }
  649.     t=r;
  650.     r=(l>>1L)|(l<<31L);
  651.     l=(t>>1L)|(t<<31L);
  652.     /* clear the top bits on machines with 8byte longs */
  653.     l&=0xffffffffL;
  654.     r&=0xffffffffL;
  655.  
  656.     PERM_OP(r,l,t, 1,0x55555555L);
  657.     PERM_OP(l,r,t, 8,0x00ff00ffL);
  658.     PERM_OP(r,l,t, 2,0x33333333L);
  659.     PERM_OP(l,r,t,16,0x0000ffffL);
  660.     PERM_OP(r,l,t, 4,0x0f0f0f0fL);
  661.  
  662.     *out0=l;
  663.     *out1=r;
  664.     return(0);
  665.     }
  666.  
  667.